<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Project;
use App\Bid;
use App\Hk;
use App\Task;
use App\Grow;
use App\Log;

class TouziController extends Controller
{
    public function gettouzi($pid){
    	$pro = Project::find($pid);
    	return view('lijitouzi',['pro' => $pro]);
    }

    public function posttouzi(request $req,$pid){
    	//生产投资记录
        //查出主键值为$pid的projects表中的数据
        /*$pro = Project::find($pid);
    	$bid = new Bid();
        $bid->uid = $req->user()->uid;
        $bid->pid = $pid;
        $bid->title = $pro->title;
        $bid->money = $req->money*100;
        $bid->pubtime = time();

        $rs0 = $bid->save();
    	//改projects表中revice字段
        $pro->revice +=$req->money*100;

        $rs1 = $pro->save();

        //投资额满,进行相关操作
        if($pro->money==$pro->revice){
            $this->tzwc($pro);
        }

        var_dump($rs0);
        var_dump($rs1);*/

        $data = [];
        $data['v_mid'] = env('PAY_SHANGHU');
        //生产订单号
        $pro = Project::find($pid);
        $log = new Log;
        $log->uid = $req->user()->uid;
        $log->pid = $pid;
        $log->title = $pro->title;
        $log->money = $req->money*100;
        $log->paytime = time();
        $log->status = 0;
        $log->save();
        
        $log->ordernumber=date('Ymd').'-'.$data['v_mid'].'-'.'0089'.$log->lid.mt_rand(1000,9999);
        $log->save();
        $data['v_oid'] = $log->ordernumber;
        $data['v_amount'] = sprintf("%01.2f", $req->money);
        $data['v_moneytype'] = 'CNY';
        $data['v_url'] = 'http://ddd.com/payyz';
        // v_amount v_moneytype v_oid v_mid v_url key
        $str=$data['v_amount'].$data['v_moneytype'].$data['v_oid'].$data['v_mid'].$data['v_url'].env('PAY_KEY');
        $data['v_md5info'] = strtoupper(md5($str));


        return view('pay',['data'=>$data,'pro'=>$pro]);
    }

    public function tbyz(request $req){
        $log = Log::where('ordernumber',$req->v_oid)->first();
        if(($result=$this->payyz($log,$req))===true){

            //生产投资记录
            //查出主键值为$pid的projects表中的数据

            $bid = new Bid();
            $bid->uid = $log->uid;
            $bid->pid = $log->pid;
            $bid->title = $log->title;
            $bid->money = $log->money;
            $bid->pubtime = time();

            $rs0 = $bid->save();
            //改projects表中revice字段
            $pro = Project::find($log->pid);
            $pro->revice +=$log->money;

            $rs1 = $pro->save();

            //投资额满,进行相关操作
            if($pro->money==$pro->revice){
                $this->tzwc($pro);
            }

            var_dump($rs0);
            var_dump($rs1);
        }else{
            return $result;
        }
    }

    public function payyz($log,$req){
        if(empty($log)){
            return '支付信息不存在';
        }
        if ($req->v_pstatus!='20') {
            return '支付失败';
        }
        if($req->v_pstring!='支付完成'){
            return '支付未完成';
        }
        $v_amount=sprintf("%01.2f", $log->money/100);
        $md5=strtoupper(md5($req->v_oid.$req->v_pstatus.$v_amount.'CNY'.env('PAY_KEY')));

        if ($req->v_md5str!=$md5) {
            return 'md5校验错误';
        }
        if($req->v_amount!=($log->money/100)){
            return '支付金额错误';
        }
        return true;
    }

    public function tzwc($pro){
        //修改项目状态
        $pro->status = 3;
        $pro->save();
        //生成还款任务
        //算出每月需要还的金额
        $amount = $pro->money/$pro->hrange+$pro->money*$pro->rate/1200;
        for ($i=0; $i < $pro->hrange; $i++) { 
            $hk = new Hk;
            $hk->uid = $pro->uid;
            $hk->pid = $pro->pid;
            $hk->title = $pro->title;
            $hk->amount = $amount;
            //算出还款日期
            $date = date('Y-m-d',strtotime('+'.($i+1).' months'));
            $hk->paydate = $date;
            $hk->status = 0;

            $hk->save();
        }
        
        //生成预收益表
        //查出此项目所有投资数据
        $bids = Bid::where('pid',$pro->pid)->get();
        //计算一年天数
        $now = time();
        $days = (strtotime('+1 year',$now)-$now)/3600/24;
        foreach ($bids as $bid) {
            $task = new Task;
            $task->bid = $bid->bid;
            $task->uid = $bid->uid;
            $task->title = $bid->title;
            //计算结束日期
            $date = date('Y-m-d',strtotime('+'.$pro->hrange.' months'));
            $task->enddate = $date;
            //计算每天应得利息
            $interest = $bid->money*$pro->rate/100/$days;
            $task->interest = $interest;

            $task->save();
        }
        



    }
    public function grow(){
        //给今天需要涨利的用户发钱
        $today = date('Y-m-d');
        $tasks = Task::where('enddate','>=',$today)->get();
        $data = [];
        $i=0;
        foreach ($tasks as $task) {
            $data[$i]['bid'] = $task->bid;
            $data[$i]['uid'] = $task->uid;
            $data[$i]['title'] = $task->title;
            $data[$i]['interest'] = $task->interest;
            $data[$i]['growdate'] = $today;
            $i+=1;
        }
        Grow::insert($data);
    }
}
